package com.rs.controller;

import com.rs.pojo.Books;
import com.rs.service.BookService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BooksController {
    //Controller调用Service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allbook")
    public String books(Model model){
        List<Books> books = bookService.queryAll();
        model.addAttribute("list",books);
        return "allbook";
    }

    @RequestMapping("/toaddbook")
    public String toaddbook(){
        return "addbook";
    }

    //添加书籍的请求
    @RequestMapping("/addbook")
    public String addbook(Books books){
        System.out.println(books);
        bookService.addBooks(books);
        return "redirect:/book/allbook";
    }

    @RequestMapping("/toupdatebook")
    public String toupdatebook(int id,Model model){
        Books books = bookService.queryById(id);
        model.addAttribute("book",books);
        return "updatebook";
    }

    //修改书籍
    @RequestMapping("/updatebook")
    public String updatebook(Books books){
        bookService.updateBooks(books);
        return "redirect:/book/allbook";
    }

    //删除书籍
    @RequestMapping("/deletebook/{bookid}")
    public String deletebook(@PathVariable("bookid") int id){
        bookService.deleteBooks(id);
        return "redirect:/book/allbook";
    }

    //查询书籍
    @RequestMapping("/query")
    public String querybook(String qname,Model model){
        List<Books> books = bookService.queryBooksByName(qname);
        model.addAttribute("list",books);
        return "/allbook";
    }
}
